package com.quectel.taskserver.job.scheduling;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.business.common.scheduling.SchedulingLeaveBusiness;
import com.quectel.business.common.schedulingapply.SchedulingApplyBusiness;
import com.quectel.constant.core.scheduling.SchedulingAttendanceDailyConstants;
import com.quectel.constant.core.scheduling.SchedulingLeaveConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.dept.dto.DeptPersonDto;
import com.quectel.core.module.dept.service.DeptPersonService;
import com.quectel.core.module.scheduling.dto.SchedulingAttendanceDailyDto;
import com.quectel.core.module.scheduling.dto.SchedulingWorkOvertimeConfigDto;
import com.quectel.core.module.scheduling.service.SchedulingAttendanceDailyService;
import com.quectel.core.module.schedulingapply.dto.vo.SchedulingTimeVO;
import com.quectel.util.common.DateUtils;
import com.quectel.util.common.Response;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/8/4 13:24
 * @description new java file header...
 */
@Component
public class SchedulingOvertimeConvertedToBalanceTask implements SimpleJob {

    private static final Logger LOGGER = LoggerFactory.getLogger(SchedulingOvertimeConvertedToBalanceTask.class);

    @DubboReference
    private SchedulingAttendanceDailyService schedulingAttendanceDailyService;
    @DubboReference
    private DeptPersonService deptPersonService;
    @Autowired
    private SchedulingApplyBusiness schedulingApplyBusiness;
    @Autowired
    private SchedulingLeaveBusiness schedulingLeaveBusiness;

    @Override
    public void execute(ShardingContext shardingContext) {
        contert();
    }

    private void contert() {
        Date now = new Date();
        //昨天的日期
        Date queryDate = DateUtil.offsetDay(now, -1);
        //查询昨天的考勤记录
        Map<String, Object> params = new HashMap<>();
        params.put(LambdaUtil.getFieldName(SchedulingAttendanceDailyDto::getDate), DateUtils.format(queryDate, DateUtils.FormatType.SIMPLE));
        List<SchedulingAttendanceDailyDto> schedulingAttendanceDailyDtos = schedulingAttendanceDailyService.queryList(params);

        if (CollectionUtils.isEmpty(schedulingAttendanceDailyDtos)) {
            return;
        }

        for (SchedulingAttendanceDailyDto schedulingAttendanceDailyDto : schedulingAttendanceDailyDtos) {

            DeptPersonDto deptPersonDto = deptPersonService.selectById(schedulingAttendanceDailyDto.getPersonId());
            if (deptPersonDto == null) {
                continue;
            }
            Response<SchedulingWorkOvertimeConfigDto> overtimeConfigByPerson = schedulingApplyBusiness.getOvertimeConfigByPerson(deptPersonDto.getId());
            if (!overtimeConfigByPerson.isSuccess() || overtimeConfigByPerson.getData() == null) {
                continue;
            }
            SchedulingTimeVO schedulingTimeVO = new SchedulingTimeVO();

            schedulingTimeVO.setTimeUnit(SchedulingLeaveConstants.TimeUnitEnum.HOUR.getUnit());

            SchedulingWorkOvertimeConfigDto data = overtimeConfigByPerson.getData();
            if (SystemConstants.NO.equals(data.getStatus())) {
                continue;
            }

            if (SchedulingAttendanceDailyConstants.SituationTypeEnum.WORK.getType().equals(schedulingAttendanceDailyDto.getSituation())) {
                SchedulingWorkOvertimeConfigDto.WorkDayConfig workDayConfig = data.getWorkDayConfig();
                if (workDayConfig == null || SystemConstants.NO.equals(workDayConfig.getStatus())) {
                    continue;
                }

                //仅做记录
                if (SystemConstants.NO.equals(workDayConfig.getSettlementType())) {
                    continue;
                }
                //配置不为空
                if (workDayConfig.getRate() != null && workDayConfig.getDeductionHolidayType() != null) {
                    BigDecimal multiply = schedulingAttendanceDailyDto.getWorkOvertimeHour().multiply(BigDecimal.valueOf(workDayConfig.getRate()));
                    schedulingTimeVO.setTimeLength(multiply);
                    schedulingLeaveBusiness.leaveBalanceUpdate(
                            deptPersonDto.getId(),
                            schedulingAttendanceDailyDto.getDate(),
                            schedulingAttendanceDailyDto.getId(),
                            schedulingTimeVO,
                            workDayConfig.getDeductionHolidayType()
                    );
                }


            } else if (SchedulingAttendanceDailyConstants.SituationTypeEnum.REST.getType().equals(schedulingAttendanceDailyDto.getSituation())) {
                SchedulingWorkOvertimeConfigDto.RestDayConfig restDayConfig = data.getRestDayConfig();
                if (restDayConfig == null || SystemConstants.NO.equals(restDayConfig.getStatus())) {
                    continue;
                }

                //仅做记录
                if (SystemConstants.NO.equals(restDayConfig.getSettlementType())) {
                    continue;
                }
                //配置不为空
                if (restDayConfig.getRate() != null && restDayConfig.getDeductionHolidayType() != null) {
                    BigDecimal multiply = schedulingAttendanceDailyDto.getWorkOvertimeHour().multiply(BigDecimal.valueOf(restDayConfig.getRate()));
                    schedulingTimeVO.setTimeLength(multiply);
                    schedulingLeaveBusiness.leaveBalanceUpdate(
                            deptPersonDto.getId(),
                            schedulingAttendanceDailyDto.getDate(),
                            schedulingAttendanceDailyDto.getId(),
                            schedulingTimeVO,
                            restDayConfig.getDeductionHolidayType()
                    );
                }
            } else if (SchedulingAttendanceDailyConstants.SituationTypeEnum.VACATION.getType().equals(schedulingAttendanceDailyDto.getSituation())) {
                SchedulingWorkOvertimeConfigDto.HolidayDayConfig holidayConfig = data.getHolidayConfig();
                if (holidayConfig == null || SystemConstants.NO.equals(holidayConfig.getStatus())) {
                    continue;
                }

                //仅做记录
                if (SystemConstants.NO.equals(holidayConfig.getSettlementType())) {
                    continue;
                }
                //配置不为空
                if (holidayConfig.getRate() != null && holidayConfig.getDeductionHolidayType() != null) {
                    BigDecimal multiply = schedulingAttendanceDailyDto.getWorkOvertimeHour().multiply(BigDecimal.valueOf(holidayConfig.getRate()));
                    schedulingTimeVO.setTimeLength(multiply);
                    schedulingLeaveBusiness.leaveBalanceUpdate(
                            deptPersonDto.getId(),
                            schedulingAttendanceDailyDto.getDate(),
                            schedulingAttendanceDailyDto.getId(),
                            schedulingTimeVO,
                            holidayConfig.getDeductionHolidayType()
                    );
                }
            }

        }


    }
}
